package com.hxd.mall.utils.open;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.util.Log;
import android.widget.Toast;

import java.util.List;

/**
 * Created by 尹正伟 on 17/6/19.
 */

public class LancherAppUtils {

    public static boolean isInstall(String packageName, Activity mContext) {
        try {
            mContext.getPackageManager().getPackageInfo(packageName, 0);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            Log.d(packageName, "找不到指定包信息");
            Toast.makeText(mContext, "应用未安装", Toast.LENGTH_SHORT).show();
            return false;
        }
    }

    public static void openCLD(String packageName, Activity mContext) {
        if (!isInstall(packageName, mContext)) {
            Toast.makeText(mContext, "应用未安装", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            PackageManager packageManager = mContext.getPackageManager();
            PackageInfo pi = packageManager.getPackageInfo(packageName, 0);
            Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
            resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            resolveIntent.setPackage(pi.packageName);
            List<ResolveInfo> apps = packageManager.queryIntentActivities(resolveIntent, 0);
            ResolveInfo ri = apps.iterator().next();
            if (ri != null) {
                String className = ri.activityInfo.name;

                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                ComponentName cn = new ComponentName(pi.packageName, className);

                intent.setComponent(cn);
                mContext.startActivityForResult(intent, 0);
            }
            Log.d("packageName", packageName);
        } catch (Exception e) {
            Log.d(packageName, "找不到指定包信息");
        }
    }

    /**
     * 结束进程
     */
    public static int killProcess(String packageName) {
        try {
            Process exec = Runtime.getRuntime().exec("adb shell am force-stop " + packageName);
            return exec.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public static String getRunningActivityName(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        CharSequence runningActivity = activityManager.getRunningTasks(1).get(0).topActivity.getClassName();
        Log.d("getRunningActivityName", runningActivity.toString());
        return runningActivity.toString();
    }

    public static String getRunningPackName(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        CharSequence runningActivity = activityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
        Log.d("getRunningPackName", runningActivity.toString());
        return runningActivity.toString();
    }
}
